/*******************************************************************
 *
 * Copyright (C), 2021-2022, Letstrong, All rights reserved.
 *
 * Author      : 
 * Create Time : 2020-12-17
 * Version     : v0.1
 * Description : 
 *******************************************************************/

#ifndef __APP_STORE_MEM_H__
#define __APP_STORE_MEM_H__

/*----------------------------Head file----------------------------*/

#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "hal_flash.h"
#include "app_global_data.h"

#ifdef __cplusplus
 extern "C" {
#endif

/*----------------------------macro file---------------------------*/

#define INDEX_HEADER	0xFEFDFCFB

#define DATA_SAVE_HEAD1		'L'
#define DATA_SAVE_HEAD2		'S'
#define DATA_SAVE_HEAD3		'E'

#define FLASH_SECTOR_SIZE	HAL_FLASH_SECTOR_SIZE

/*----------------------------type define--------------------------*/

#pragma pack(1)

typedef struct{
	uint32_t header;
	uint32_t latest_addr;
	uint32_t oldest_addr;
}index_content_t;

typedef struct{
	bool addr_valid;
	const uint32_t cache_max_size;		//Sample data saved max size
	const uint16_t data_size;
	const uint32_t index_start_addr;	//Index start border address
	const uint32_t index_end_addr;		//Index end border address
	const uint32_t data_start_addr;		//Data start border address
	const uint32_t data_end_addr;		//Data end border address
	uint32_t tmp_addr;

	uint32_t index_addr;				//The latest index address, contain the newest latest/oldest data address.
	uint32_t latest_addr;				//Latest data address
	uint32_t oldest_addr;				//Oldest data address
}index_data_t;

typedef struct{
	uint8_t  byte1;
	uint8_t  byte2;
	uint8_t  byte3;
}save_common_head_t;			//3 bytes


typedef struct{
	save_common_head_t head;
	skip_packet_t skip;
	uint8_t unused[1];
}save_skip_packet_t;		//224 bytes, user store operate addr must be x*4


#pragma pack()

/*----------------------------var define---------------------------*/

/*-------------------------func declaration------------------------*/
extern void app_mem_data_init(void);
extern void app_mem_skip_data_save(void);
extern void app_mem_data_clear_all(void);
extern void app_store_mem_data_revert(void);
extern void app_mem_sent_data_abandon(void);
extern uint32_t app_mem_unsent_data_size_get(void);
extern void app_mem_latest_skip_ret_read(uint8_t* pData, uint16_t size);
extern void app_mem_unsent_skip_data_read(uint8_t* pData, uint16_t size);

/*-----------------------------------------------------------------*/
	 
#ifdef __cplusplus
}
#endif

#endif
